from django.shortcuts import render, redirect
from django.views import View
from django.contrib.auth import logout


from . import forms
from libs.response import json_response

# Create your views here.


class LoginView(View):
    """登录视图"""

    def get(self, request):
        return render(request, "login.html")

    def post(self, request):
        form = forms.LoginForm(request.POST, request=request)

        if form.is_valid():
            return json_response(msg='登录成功')
        else:
            # 将表单错误信息进行拼接
            err_msg_str = list(form.errors.values())[0]

            return json_response(msg=err_msg_str)


class UserLogoutView(View):
    """用户登出视图"""

    def get(self, request):
        logout(request)
        return redirect("home:home")
